﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Pickle.Euler.Problems
{
	/// <summary>
	/// 
	/// </summary>
	struct Triplet
	{
		/// <summary>
		/// 
		/// </summary>
		public int a { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public int b { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public int c { get; set; }

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public int Sum { get { return a + b + c; } }

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public int Product { get { return a * b * c; } }

		/// <summary>
		/// 
		/// </summary>
		public bool IsTriplet { get { return (a * a) + (b * b) == (c * c); } }

		/// <summary>
		/// 
		/// </summary>
		public bool IsTarget { get { return IsTriplet && Sum == 1000; } }

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return "a,b,c (" + a + "," + b + "," + c + ")" + Environment.NewLine
				+ "IsTriplet(" + IsTriplet + ")" + Environment.NewLine
				+ "Sum(" + Sum + ")" + Environment.NewLine
				+ "Product(" + Product + ")";
		}
	}

	/// <summary>
	/// 
	/// </summary>
	public class Problem009 : BaseProblem
	{
		/// <summary>
		/// 
		/// </summary>
		public Problem009()
		{
			Question = @"A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^(2) + b^(2) = c^(2)

For example, 3^(2) + 4^(2) = 9 + 16 = 25 = 5^(2).

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.";
			ProblemNumber = 9;
		}

		/// <summary>
		/// 
		/// </summary>
		public override string Answer
		{
			get
			{
				Triplet Trip = new Triplet();

				do
				{
					if (Trip.IsTarget == true)
						break;

					// for each change in a
					// increment a
					Trip.a++;

					// b = a + 1
					Trip.b = Trip.a + 1;

					// c = b + 1
					Trip.c = Trip.b + 1;

					// if we're at the magic value, bail
					if (Trip.IsTarget == true)
						break;

					// try each b (b will try c)
					do
					{
						do
						{
							// do we have a winner
							if (Trip.IsTarget == true)
								break;

							Trip.c++;
						} while (Trip.c < 1000);

						// did c hook us up?
						if (Trip.IsTarget == true)
							break;

						Trip.b++;
						Trip.c = Trip.b + 1;
					} while (Trip.b < 500);
				} while (Trip.IsTarget == false && Trip.a < 1000);

				Debug.WriteLine(Trip);

				return Trip.Product.ToString();
			}
		}
	}
}
